//
//  XianhuoListCell.swift
//  YunshuApp
//
//  Created by 周文杰 on 2017/6/7.
//  Copyright © 2017年 zwj. All rights reserved.
//

import UIKit

class XianhuoListCell: UITableViewCell {

    let locationImg = UIImageView.init()
    let categoryTV = UILabel.init()
    let imgView = UIImageView.init()
    let companyImgView = UIImageView.init()
    let dateTV = UILabel.init()
    let locationTV = UILabel.init()
    //        View companyLayout;
    let companyTV = UILabel.init()
    let factoryTV = UILabel.init()
    let bottomMargin = UIView.init()
//    let priceTV = UILabel.init()
//    let specificationTV = UILabel.init()
//    let qualityTV = UILabel.init()
    
    let marginTopBottom = 5
    let line1 = UIView.init(frame: CGRect.init(x: 0, y: 5, width: Const.SCREEN_WIDTH, height: Const.ONE_PIXEL))
    let line2 = UIView.init(frame: CGRect.init(x: 0, y: 5+45, width: Const.SCREEN_WIDTH, height: Const.ONE_PIXEL))
    //5+45+10+24+26*4+5
    let line3 = UIView.init(frame: CGRect.init(x: 0, y:153 , width: Const.SCREEN_WIDTH, height: Const.ONE_PIXEL))
    
    static let height:CGFloat = 158.0
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    
        line1.backgroundColor = Const.WTM_LIGHTGRAY
        self.contentView.addSubview(line1)
        line2.backgroundColor = Const.WTM_LIGHTGRAY
        self.contentView.addSubview(line2)
        line3.backgroundColor = Const.WTM_LIGHTGRAY
        self.contentView.addSubview(line3)
        
        categoryTV.frame = CGRect.init(x: 10, y: marginTopBottom, width: 100, height: 45)
        categoryTV.font = UIFont.systemFont(ofSize: 12)
        self.contentView.addSubview(categoryTV)
        
        let offset = self.marginTopBottom + 15;
        locationImg.frame = CGRect.init(x: Int(Const.SCREEN_WIDTH - 70), y: marginTopBottom + 13 , width: 18, height: 18)
        locationImg.image = UIImage.init(named: "xh_location")
        self.contentView.addSubview(locationImg)
        
        locationTV.frame = CGRect.init(x: Int(Const.SCREEN_WIDTH - 45), y: marginTopBottom, width: 50, height: 45)
        locationTV.textColor = Const.WTM_GRAY
        locationTV.font = UIFont.systemFont(ofSize: 12)
        self.contentView.addSubview(locationTV)
        
        imgView.frame = CGRect.init(x: 10, y: marginTopBottom+45+15, width: 60, height: 80)
        imgView.backgroundColor = Const.WTM_WHITE_SMOKE
        imgView.layer.cornerRadius = 4
        imgView.contentMode = .scaleAspectFit
        self.contentView.addSubview(imgView)
        
        companyImgView.frame = CGRect.init(x: 80, y: offset + 55, width: 24, height: 24)
        companyImgView.image = UIImage.init(named: "xh_company")
        self.contentView.addSubview(companyImgView)
        
        companyTV.frame = CGRect.init(x: 110, y: offset + 55, width: Int(Const.SCREEN_WIDTH - 110), height: 24)
        companyTV.font = UIFont.systemFont(ofSize: 14)
        self.contentView.addSubview(companyTV)
        
        factoryTV.frame = CGRect.init(x: 85, y: offset + 84, width: Int(Const.SCREEN_WIDTH - 80), height: 16)
        factoryTV.font = UIFont.systemFont(ofSize: 12)
        factoryTV.textColor = Const.WTM_GRAY
        self.contentView.addSubview(factoryTV)
        
//        specificationTV.frame = CGRect.init(x: 85, y: offset + 105, width: Int(Const.SCREEN_WIDTH - 80), height: 16)
//        specificationTV.font = UIFont.systemFont(ofSize: 12)
//        specificationTV.textColor = Const.WTM_GRAY
//        self.contentView.addSubview(specificationTV)
//        
//        qualityTV.frame = CGRect.init(x: 85, y: offset + 125, width: Int(Const.SCREEN_WIDTH - 80), height: 16)
//        qualityTV.font = UIFont.systemFont(ofSize: 12)
//        qualityTV.textColor = Const.WTM_GRAY
//        self.contentView.addSubview(qualityTV)
//        
//        priceTV.frame = CGRect.init(x: Int(Const.SCREEN_WIDTH - 160), y: offset + 125, width: 150, height: 16)
//        priceTV.font = UIFont.systemFont(ofSize: 12)
//        priceTV.textColor = Const.WTM_GRAY
//        priceTV.textAlignment = .right
//        self.contentView.addSubview(priceTV)
        
        dateTV.frame = CGRect.init(x: 85, y: offset + 105, width: Int(Const.SCREEN_WIDTH - 80), height: 13)
        dateTV.font = UIFont.systemFont(ofSize: 10)
        dateTV.textColor = Const.WTM_GRAY
        self.contentView.addSubview(dateTV)
        
        let topMargin = UIView.init(frame: CGRect.init(x: 0, y: 0, width: Int(Const.SCREEN_WIDTH), height: marginTopBottom))
        topMargin.backgroundColor = Const.WTM_WHITE_SMOKE
        self.contentView.addSubview(topMargin)

        let y = Int(line3.frame.origin.y + Const.ONE_PIXEL*2)
        bottomMargin.frame = CGRect.init(x: 0, y: y, width: Int(Const.SCREEN_WIDTH), height: marginTopBottom)
        bottomMargin.backgroundColor = Const.WTM_WHITE_SMOKE
        self.contentView.addSubview(bottomMargin)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

//    func setPrice(_ p : String) -> Void{
//        let intString = self.getIntFromString(p)
//        if !p.starts(with: intString) {
//            priceTV.text = p
//            return
//        }
//        let price = NSMutableAttributedString.init(string: p)
//        price.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.red], range:NSMakeRange(0, intString.length))
//        priceTV.attributedText = price
//    }
    
    func getIntFromString(_ str:String) -> String {
        let scanner = Scanner(string: str)
        scanner.scanUpToCharacters(from: CharacterSet.decimalDigits, into: nil)
        var number :Int = 0
        
        scanner.scanInt(&number)
        
        print(number)
        return String(number)
        
    }
}
